<template>
  <el-card>
    <div slot="header">
      <span>商品添加</span>
    </div>
    <div class="content">
      <el-form
        size="small"
        label-width="100px"
        :rules="rules"
        :model="goodsName"
        ref="goodsName"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="goodsName.name" class="goods-name"></el-input>
        </el-form-item>

        <el-form-item label="商品分类" prop="category">
          <el-select v-model="goodsName.category">
            <el-option v-for="v in options" :key="v" :value="v">{{
              v
            }}</el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="商品价格" prop="price">
          <el-input-number v-model="goodsName.price"></el-input-number>
        </el-form-item>

        <el-form-item label="商品图片" prop="imgUrl">
          <el-upload
            class="avatar-uploader"
            action="http://127.0.0.1:5000/goods/goods_img_upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img
              v-if="goodsName.imgUrl"
              :src="goodsName.imgUrl"
              class="avatar"
            />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="商品描述" prop="goodsDesc">
          <el-input
            v-model="goodsName.goodsDesc"
            type="textarea"
            rows="5"
            class="goods-dec"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit('goodsName')"
            >添加商品</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </el-card>
</template>

<script>
import { UpdateGoodsCategories, goodsAdd } from "@/api/goods";
export default {
  data() {
    return {
      goodsName: {
        name: "",
        category: "",
        price: "",
        imgUrl: "",
        goodsDesc: "",
      },
      //验证规则
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },

        ],
        category: [{  required: true,message: "请输入商品分类", trigger: "change" }],
        price: [{ required: true, message: "请输入商品价格", trigger: "change" }],
        imgUrl: [{  required: true,message: "请输入图片连接", trigger: "blur" }],
        goodsDesc: [{  required: true,message: "请输入相关描述", trigger: "blur" }],
      },
      //分类名选项
      options: [],
    };
  },
  methods: {
    //获取数据
    async getData() {
      let res = await UpdateGoodsCategories();
      console.log(res.data);
      //赋值渲染
      this.options = res.data;
    },
    //上传图片
    //上传成功，后端响应数据，接收后端响应的数据
    handleAvatarSuccess(res) {
      console.log(res);
      this.goodsName.imgUrl = res.imgUrl;
    },

    //图像上传之前触发 限制图片格式大小
    beforeAvatarUpload(file) {
      //如果文件的后缀是jpeg  isJPG就是true 否则就是false
      const isJPG = file.type === "image/jpeg";
      //如果文件小于2M isLt2m就是true 否则就是false
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      //两者都满足才能上传成功
      return isJPG && isLt2M;
    },
    //点击保存添加的商品
    onSubmit(goodsName) {
      console.log("kkkkk", this.$refs[goodsName]);
      this.$refs[goodsName].validate((valid) => {
        console.log(valid);
      });

       this.$refs[goodsName].validate(async(valid) => {
        console.log(valid)
         if(valid){
          let res =   await goodsAdd(this.goodsName)
          let {msg} = res.data
          if(res){
            this.$message.success(msg)
            this.$router.push('/goods/goods-list')
          }
         }else{
          this.$message.error(msg)
         }
        }
        );
    },
  },
  created() {
    this.getData();
  },
};
</script>

<style lang="less" scoped>
.goods-name {
  width: 194px;
}
.goods-dec {
  width: 300px;
}

/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>